﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using QuTouTiaoAssist.Entity;
using QuTouTiaoAssist.Util;

namespace QuTouTiaoAssist
{
    public partial class FrmBase : Form
    {
        protected Dictionary<Control, ControlTextValidate> ValidateRules = new Dictionary<Control, ControlTextValidate>();

        protected string MemeberToken
        {
            get { return GlobalContext.MemberToken; }
        }

        protected QttMemberInfo MemberInfo
        {
            get { return GlobalContext.MemberInfo; }
        }

        protected string AppName
        {
            get { return Assembly.GetExecutingAssembly().GetName().Name; }
        }

        public FrmBase()
        {
            InitializeComponent();
        }

        protected bool CheckLogin()
        {
            return GlobalContext.CheckLogin(true);
        }

        protected void InvokeCrossThread<T>(Action<T> action, params object[] args)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(action, args);
                return;
            }
            action((T)args[0]);
        }

        protected void EnableCancelButton()
        {
            UiUtil.SetCancelButton(this);
        }

        protected void ShowTextBoxPwd(TextBox txt)
        {
            txt.PasswordChar = '\0'; // 表示显示明文
        }

        protected void SetControlText(Control control, string text)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new Action<Control, string>(SetControlText), control, text);
                return;
            }
            control.Text = text;
        }

        protected void SetControlEnabled(Control control, bool enabled)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new Action<Control, bool>(SetControlEnabled), control, enabled);
                return;
            }
            control.Enabled = enabled;
        }

        protected void ChangeMainTitle(string str)
        {
            GlobalContext.ChangeMainTitle(str);
        }

        protected void AppendListViewItem(ListView lv, object[] objs)
        {
            AppendListViewItem(lv, objs.Select(
                                        selector => selector.ToString()).ToArray());
        }

        protected void AppendListViewItem(ListView lv, string[] objs)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new Action<ListView, string[]>(AppendListViewItem), lv, objs);
                return;
            }

            lv.Items.Add(new ListViewItem(objs));
        }

        protected void ClearListView(ListView lv)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new Action<ListView>(ClearListView), lv);
                return;
            }

            lv.Items.Clear();
        }

        protected void SetListViewSubItemText(ListView lv, int rowIndex, int colIndex, string info)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new Action<ListView, int, int, string>(SetListViewSubItemText), lv, rowIndex, colIndex, info);
                return;
            }

            lv.Items[rowIndex].SubItems[colIndex].Text = info;
        }

        protected bool CheckValidateRule(Control control)
        {
            if (this.ValidateRules.ContainsKey(control))
            {
                var result = this.ValidateRules[control].IsValid();
                if (result == false)
                {
                    MsgBox.ShowInfo(this.ValidateRules[control].ErrorMsg);
                }
                return result;
            }

            return false;
        }
    }
}
